import router from "@/router";
import Axios from "axios";

const request = Axios.create({
  baseURL: "http://localhost:8080/",
  headers: { "content-type": "application/json;charset=utf-8" },
});

// request拦截器
request.interceptors.request.use(
  (config) => {
    config.headers["token"] = localStorage.getItem("token");
    if (
      localStorage.getItem("token") == null && config.url.indexOf("/login")==-1
      // token如果存在并且不是登录接口，则统一在http请求的header都加上token，这样后台根据token判断你的登录情况，
      //在这里插一嘴axios的请求设置头name后台接收时必须也设置接收这个请求头name（一一对应）,否者会报415
    ) {
      alert("未登录");
      router.push({ name: "login" });
    } else {
      
      return config;
    }
  
  },
  (error) => {
    return Promise.reject(error);
  }
);

// response拦截器
//   request.interceptors.response.use(response => {
//     if (response.data && response.data.code === 401) { // 401, token失效
//       Vue.cookie.delete('token')
//       router.push({name: 'login'})
//     }
//     return response
//   }, error => {
//     return Promise.reject(error)
//   })

export default request;
